package com.flowable.convert;

import cn.hutool.core.map.MapUtil;
import com.common.core.domain.entity.SysDept;
import com.common.core.domain.entity.SysUser;
import com.common.utils.CollectionUtils;
import com.common.utils.bean.BeanUtils;
import com.flowable.common.FlowableUtils;
import com.flowable.common.NumberUtils;
import com.flowable.common.enums.TaskStatusEnum;
import com.flowable.controller.oa.vo.AdminUserRespDTO;
import com.flowable.controller.oa.vo.UserSimpleBaseVO;
import com.flowable.domain.vo.BpmTaskRespVO;
import com.flowable.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

import static com.common.utils.CollectionUtils.convertList;

@Mapper
public interface BpmTaskConvert {


    BpmTaskConvert INSTANCE = Mappers.getMapper(BpmTaskConvert.class);

    default  List<BpmTaskRespVO> buildTaskListByProcessInstanceId(List<HistoricTaskInstance> taskList, Map<Long, SysUser> userMap, Map<Long, SysDept> deptMap) {
        return convertList(taskList, task -> {
            // 特殊：已取消的任务，不返回
            BpmTaskRespVO taskVO = BeanUtils.toBean(task, BpmTaskRespVO.class);
            Integer taskStatus =FlowableUtils.getTaskStatus(task);
            if (TaskStatusEnum.isCancelStatus(taskStatus)) {
                return null;
            }
            taskVO.setStatus(taskStatus);
            taskVO.setReason((String) task.getTaskLocalVariables().get("TASK_REASON"));
            // 表单信息

            // 用户信息
            buildTaskAssignee(taskVO, task.getAssignee(), userMap, deptMap);
            buildTaskOwner(taskVO, task.getOwner(), userMap, deptMap);
            return taskVO;
        });
    }
    default   void buildTaskAssignee(BpmTaskRespVO task, String taskAssignee,
                                   Map<Long, SysUser> userMap,
                                   Map<Long, SysDept> deptMap) {
        SysUser assignUser = userMap.get(Long.parseLong( taskAssignee));
        if (assignUser != null) {
            task.setAssigneeUser(BeanUtils.toBean(assignUser, UserSimpleBaseVO.class));

        }
    }
    default   void buildTaskOwner(BpmTaskRespVO task, String taskOwner,
                        Map<Long, SysUser> userMap,
                        Map<Long, SysDept> deptMap) {
        SysUser ownerUser = userMap.get(taskOwner);
        if (ownerUser != null) {
            task.setOwnerUser(BeanUtils.toBean(ownerUser, UserSimpleBaseVO.class));

        }
    }

    default BpmTaskRespVO buildTodoTask(Task todoTask, List<Task> childrenTasks,
                                        Map<Integer, BpmTaskRespVO.OperationButtonSetting> buttonsSetting
                                       ) {
        BpmTaskRespVO bpmTaskRespVO = BeanUtils.toBean(todoTask, BpmTaskRespVO.class);
        bpmTaskRespVO.setStatus(FlowableUtils.getTaskStatus(todoTask));
        bpmTaskRespVO.setReason(FlowableUtils.getTaskReason(todoTask));
        bpmTaskRespVO.setButtonsSetting(buttonsSetting);

        bpmTaskRespVO.setChildren(
                convertList(childrenTasks, childTask -> BeanUtils.toBean(childTask, BpmTaskRespVO.class))
               // bpmTaskRespVO.setStatus(FlowableUtils.getTaskStatus(childTask)))
        );
       // bpmTaskRespVO.setStatus(FlowableUtils.getTaskStatus(childTask));
//        if (form != null) {
//            bpmTaskRespVO.setFormId(form.getId()).setFormName(form.getName())
//                    .setFormConf(form.getConf()).setFormFields(form.getFields());
//        }
        return bpmTaskRespVO;
    }

    default BpmMessageSendWhenTaskCreatedReqDTO convert(ProcessInstance processInstance, SysUser startUser,
                                                        Task task) {
        BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
        reqDTO.setProcessInstanceId(processInstance.getProcessInstanceId());
        reqDTO.setProcessInstanceName(processInstance.getName());
        reqDTO.setStartUserId(startUser.getUserId());
        reqDTO.setStartUserNickname(startUser.getNickName());
        reqDTO.setTaskId(task.getId());
        reqDTO.setTaskName(task.getName());
        reqDTO.setAssigneeUserId(NumberUtils.parseLong(task.getAssignee()));
        return reqDTO;
    }
}
